﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using CrystalDecisions.CrystalReports.Engine;
using CUSTOMS.Modules.QLHS.Components.QLBB;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Collections;
using CUSTOMS.Components;
using System.Web.UI;

namespace CUSTOMS.Modules.QLHS.Controls
{
    public partial class TheoDoiBienBan : ModulePage
    {
        protected string GetBienBanStatusImageUrl(object status)
        {
            if (status.ToString().ToUpper().Equals("CHƯA HỒI BÁO"))
            {
                return "~/Images/Icons/QLHS.BienBan.Send.gif";
            }
            return "~/Images/Icons/QLHS.BienBan.Received.gif";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                DateTime today = DateTime.Today;
                DateTime lastWeek = today.Subtract(new TimeSpan(7, 0, 0, 0));
                txtTuNgay.Text = lastWeek.ToShortDateString();
                txtDenNgay.Text = today.ToShortDateString();
            }
        }

        protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            gvBienBan.PageIndex = ddl.SelectedIndex;
            gvBienBan.DataBind();
        }

        protected void gvBienBan_DataBound(object sender, EventArgs e)
        {
            if (gvBienBan.Rows.Count > 0)
            {
                // Retrieve the pager row.
                GridViewRow bottomPagerRow = gvBienBan.BottomPagerRow;
                GridViewRow topPagerRow = gvBienBan.TopPagerRow;

                // Retrieve the DropDownList and Label controls from the row.
                DropDownList topPageList = (DropDownList)topPagerRow.Cells[0].FindControl("PageDropDownList");
                DropDownList bottomPageList = (DropDownList)bottomPagerRow.Cells[0].FindControl("PageDropDownList");
                Label topPageLabel = (Label) topPagerRow.Cells[0].FindControl("PageLabel");
                Label bottomPageLabel = (Label)bottomPagerRow.Cells[0].FindControl("PageLabel");

                if (topPageList != null && bottomPageList != null)
                {
                    for (int i = 0; i < gvBienBan.PageCount; i++)
                    {
                        int pageNumber = i + 1;
                        ListItem item = new ListItem(pageNumber.ToString());
                        if (i == gvBienBan.PageIndex)
                        {
                            item.Selected = true;
                        }

                        topPageList.Items.Add(item);
                        bottomPageList.Items.Add(item);
                    }
                }

                if (topPageLabel != null && bottomPageLabel != null)
                {
                    topPageLabel.Text = bottomPageLabel.Text = " / " + gvBienBan.PageCount.ToString();
                }
            }
        }

        protected void gvBienBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            Hashtable settings = this.ParentControl.Settings;

            IOrderedDictionary keys = gvBienBan.SelectedDataKey.Values;
            // Report.
            ReportDocument rpt = new ReportDocument();
            string path = Server.MapPath("~/Modules/CUSTOMS.Modules.QLHS/Reports/BienBanBanGiao.rpt");
            rpt.Load(path);

            BienBan bienban = BienBan.Load(Convert.ToInt32(keys[0]), keys[1].ToString(), keys[2].ToString(), Convert.ToInt32(keys[3]));

            DataSet dsHangHoa = HangHoa.SelectBy_ToKhai(Convert.ToInt32(keys[0]), keys[1].ToString(), keys[2].ToString(), Convert.ToInt32(keys[3]));

            rpt.SetDataSource(dsHangHoa.Tables[0]);

            rpt.SetParameterValue("CucHaiQuan", settings["CucHaiQuan"].ToString());
            rpt.SetParameterValue("HaiQuanCaiDat", settings["TenHaiQuan"].ToString());
            rpt.SetParameterValue("SoBienBan", bienban.SoBienBan);
            rpt.SetParameterValue("NgayBanGiao", bienban.NgayBanGiao);
            rpt.SetParameterValue("NoiGiao", DanhMuc.GetTenDonViHaiQuan(bienban.NoiGiao));
            rpt.SetParameterValue("NoiNhan", DanhMuc.GetTenDoanhNghiep(bienban.NoiNhan));
            rpt.SetParameterValue("NguoiNhan", bienban.NguoiNhan);
            rpt.SetParameterValue("SoVanTaiDon", bienban.SoVanTaiDon);
            rpt.SetParameterValue("SoBanChinhToKhai", bienban.SoBanChinhToKhai);
            rpt.SetParameterValue("SoBanChinhDonChuyenCuaKhau", bienban.SoBanChinhDonChuyenCuaKhau);
            rpt.SetParameterValue("SoBanSaoVanTaiDon", bienban.SoBanSaoVanTaiDon);
            rpt.SetParameterValue("SoBanChinhBanKeChiTiet", bienban.SoBanChinhBanKeChiTiet);
            rpt.SetParameterValue("TuyenDuongVanChuyen", bienban.TuyenDuongVanChuyen);
            rpt.SetParameterValue("ChieuDai", bienban.ChieuDai);
            rpt.SetParameterValue("ThoiGianVanChuyen", bienban.ThoiGianVanChuyen);
            rpt.SetParameterValue("GioXuatPhat", bienban.GioXuatPhat);
            rpt.SetParameterValue("HaiQuanChuyenDen", DanhMuc.GetTenDonViHaiQuan(bienban.MaHaiQuanChuyenDen));
            rpt.SetParameterValue("BienBanVeTinhTrangHangHoa", bienban.BienBanVeTinhTrangHangHoa);
            rpt.SetParameterValue("TinhTrangHangHoa", bienban.TinhTrangHangHoa);
            rpt.SetParameterValue("CacVanDeLienQuan", bienban.CacVanDeLienQuan);

            Session["CUSTOMS.Modules.QLHS.Report"] = rpt;
            Response.Redirect(string.Format("~/Modules/CUSTOMS.Modules.QLHS/ViewReport.aspx?tabid={0}&mid={1}", this.TabID, this.ModuleID));
        }        

        protected void gvBienBan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateStatus")
            {
                GridViewRow selectedRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                int index = Convert.ToInt32(selectedRow.RowIndex);

                IOrderedDictionary keys = gvBienBan.DataKeys[index].Values;
                int sotokhai = Convert.ToInt32(keys[0]);
                string mahaiquan = keys[1].ToString();
                string maloaihinh = keys[2].ToString();
                int namdangky = Convert.ToInt32(keys[3]);

                BienBan.ChuyenTrangThai(sotokhai, mahaiquan, maloaihinh, namdangky);
                gvBienBan.DataBind();
            }
        }

        protected void btnThongKe_Click(object sender, EventArgs e)
        {

        }
    }
}